package com.aigou.admin.aigouspringboot.service.impl;

import com.aigou.admin.aigouspringboot.common.Constants;
import com.aigou.admin.aigouspringboot.entity.Order;
import com.aigou.admin.aigouspringboot.entity.Product;
import com.aigou.admin.aigouspringboot.entity.dto.ProductQueryDTO;
import com.aigou.admin.aigouspringboot.entity.vo.OrderVO;
import com.aigou.admin.aigouspringboot.mapper.OrderMapper;
import com.aigou.admin.aigouspringboot.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public List<Order> selectAll() {
        return orderMapper.selectAll();
    }

    @Override
    public PageInfo<Order> selectOrderListByPage(Integer current) {
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 查询所有用户信息
        List<Order> orderList = orderMapper.selectAll();
        // 把userList放入返回分页信息
        return new PageInfo<>(orderList);
    }

    @Override
    public int deleteById(String id) {
        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Order selectById(String id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(Order order) {
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public int save(Order order) {
        return orderMapper.insertSelective(order);
    }

    @Override
    public PageInfo<OrderVO> selectByUserId(String userId, Integer current) {
        PageHelper.startPage(current, 3);
        List<OrderVO> orderList = orderMapper.getOrderVO(userId);
        for (OrderVO order : orderList) {
            Double price = order.getPrice();
            Integer productNum = order.getProductNum();
            order.setTotalPrice(price * productNum);
        }
        return new PageInfo<>(orderList);
    }

    @Override
    public PageInfo<OrderVO> selectProductListByPage(String userId, @RequestParam Map<String, Object> params) {
        PageHelper.startPage(1, 3);
        // 查询所有产品信息
        List<OrderVO> orderList = orderMapper.selectSalesByCondition(userId,params);
        // 把productList放入返回分页信息
        return new PageInfo<>(orderList);
    }


}
